/**
 * 
 */
package com.seguriapp.sync;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.seguriapp.communication.WebServiceConsumer;
import com.seguriapp.controllers.EntityController;
import com.seguriapp.util.AppPrefences;

/**
 * @author pjohnson
 * 
 */
public class EntityTask extends AsyncTask<Object, Boolean, Boolean> {

	private Context mContext;

	public EntityTask(final Context context) {
		mContext = context;
	}

	@Override
	protected Boolean doInBackground(Object... arg0) {

		final WebServiceConsumer webServiceConsumer = WebServiceConsumer
				.getInstance(mContext);

		try {
			final JSONObject postObject = new JSONObject(
					"{'categoriesList':'1','latitude':0.5,'longitude':0.5} ");
			String str = webServiceConsumer.getEntities(postObject);
			final JSONObject jSonStr = new JSONObject(str);
			final EntityController eController = new EntityController(mContext);
			eController
					.insertListEntities(jSonStr.getJSONArray("entitiesList"));
		} catch (JSONException e) {
			Log.e("JsonError", e.getMessage());
		}

		return true;
	}

	@Override
	protected void onPostExecute(final Boolean result) {
		if (result) {
			final AppPrefences msp = new AppPrefences(mContext);
			msp.setInitialData(true);
		}
	}
}
